package com.wind.taobao.api.jianghu.convert
{
	import com.wind.taobao.api.jianghu.model.Favorite;
	import com.wind.taobao.core.util.TaobaoConstants;

	import mx.collections.ArrayCollection;


	/**
	 * Favorite 转换器
	 * @author fxy
	 *
	 */
	public class FavoriteConvert
	{
		/**
		 * 转换Favorite集合
		 */
		public static function convertFavorites(value:Object, type:String="json"):ArrayCollection
		{
			var favorites:ArrayCollection=new ArrayCollection();
			if (value != null)
			{
				if (type == TaobaoConstants.JSON)
				{
					if ((value as Array).length > 0)
					{
						for (var i:uint=0; i < (value as Array).length; i++)
						{
							favorites.addItem(convertFavorite((value as Array)[i]));
						}
					}
				}
				else
				{
					//TODO
				}
			}
			return favorites;
		}

		/**
		 * 转换Favorite
		 */
		public static function convertFavorite(value:Object, type:String="json"):Favorite
		{
			var favorite:Favorite=new Favorite();
			if (value == null)
				return favorite;
			if (type == TaobaoConstants.XML)
			{
				favorite=convertXMLToFavorite(XML(value), favorite);
			}
			else
			{
				favorite=convertJSONToFavorite(value, favorite);
			}
			return favorite;
		}

		/**
		 * JSON转换为Favorite
		 */
		private static function convertJSONToFavorite(json:Object, favorite:Favorite):Favorite
		{
			if (json.hasOwnProperty(TaobaoConstants.RECENTLYWANT))
			{
				favorite.recentlyWant=json[TaobaoConstants.RECENTLYWANT];
			}
			if (json.hasOwnProperty(TaobaoConstants.MUSICS))
			{
				favorite.musics=json[TaobaoConstants.MUSICS];
			}
			if (json.hasOwnProperty(TaobaoConstants.GAMES))
			{
				favorite.games=json[TaobaoConstants.GAMES];
			}
			if (json.hasOwnProperty(TaobaoConstants.BOOKS))
			{
				favorite.books=json[TaobaoConstants.BOOKS];
			}
			if (json.hasOwnProperty(TaobaoConstants.BRANDS))
			{
				favorite.brands=json[TaobaoConstants.BRANDS];
			}
			if (json.hasOwnProperty(TaobaoConstants.FAVORITESONGS))
			{
				favorite.favoriteSongs=json[TaobaoConstants.FAVORITESONGS];
			}
			if (json.hasOwnProperty(TaobaoConstants.FAVORITESTAR))
			{
				favorite.favoriteStar=json[TaobaoConstants.FAVORITESTAR];
			}
			if (json.hasOwnProperty(TaobaoConstants.CATOON))
			{
				favorite.catoon=json[TaobaoConstants.CATOON];
			}
			if (json.hasOwnProperty(TaobaoConstants.INTEREST))
			{
				favorite.interest=json[TaobaoConstants.INTEREST];
			}
			if (json.hasOwnProperty(TaobaoConstants.SPORTS))
			{
				favorite.sports=json[TaobaoConstants.SPORTS];
			}
			if (json.hasOwnProperty(TaobaoConstants.FILMS))
			{
				favorite.films=json[TaobaoConstants.FILMS];
			}
			if (json.hasOwnProperty(TaobaoConstants.FAVORITEFOOD))
			{
				favorite.favoriteFood=json[TaobaoConstants.FAVORITEFOOD];
			}
			return favorite;
		}

		/**
		 * XML转换为Favorite
		 */
		private static function convertXMLToFavorite(value:XML, favorite:Favorite):Favorite
		{
			return favorite;
		}

	}
}